package net.mobgame.common;

import java.io.File;
import net.mobgame.android.HomeMessage;
import net.mobgame.android.R;
import net.mobgame.network.Preference;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.provider.Settings;

public class Utils {
	/**
	 * Check is connected to network
	 * @return true if connected
	 */
	public static boolean isOnline(Context context ) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnected()) return true;
		return false;
	}

	public static void buildAlertMessageNoInterNet(final Context ctx){
		AlertDialog.Builder adb = new AlertDialog.Builder(ctx);
		adb.setTitle(ctx.getString(R.string.notify_title));
		adb.setMessage(ctx.getString(R.string.nointernet));
		adb.setIcon(android.R.drawable.ic_dialog_info);
		adb.setCancelable(false);
		adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				ctx.startActivity(intent);
				((Activity) ctx).finish();			
			}
		});
		adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				((Activity) ctx).finish();		
			}
		});
		adb.show();
	}
	public static void buildAlertMessageNoGps(final Context context) {
		Preference mPrefer = new Preference(context);

		if(mPrefer.getNotificationGPS()){
			return;
		}
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
		.setCancelable(false)
		.setPositiveButton(context.getString(R.string.button_yes), new DialogInterface.OnClickListener() {
			public void onClick(final DialogInterface dialog, final int id) {
				context.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
			}
		})
		.setNegativeButton(context.getString(R.string.button_no), new DialogInterface.OnClickListener() {
			public void onClick(final DialogInterface dialog, final int id) {
				dialog.cancel();
			}
		});
		final AlertDialog alert = builder.create();
		mPrefer.setNotificationGPS(true);
		alert.show();
	}
	public static void buildAlertMessageNoGooglePlayService(final Context context){
		AlertDialog.Builder adb = new AlertDialog.Builder(context);
		adb.setTitle(context.getString(R.string.notify_title));
		adb.setMessage(context.getString(R.string.installgoogleplayservice));
		adb.setIcon(android.R.drawable.ic_dialog_info);
		adb.setCancelable(false);
		adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				//				Intent intent = new Intent(Intent.ACTION_VIEW, 
				//						Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.gms&hl=vi"));
				//				context.startActivity(intent);	
				((Activity) context).finish();		
			}
		});
		adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				((Activity) context).finish();	
				Intent intent = new Intent(context, HomeMessage.class);
				context.startActivity(intent);
			}
		});
		adb.show();
	}
	public static boolean hasStorage(boolean requireWriteAccess) {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			if (requireWriteAccess) {
				boolean writable = checkFsWritable();
				return writable;
			}else return true;
		} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) return true;
		return false;
	}

	private static boolean checkFsWritable() {
		// Create a temporary file to see whether a volume is really writeable.
		// It's important not to put it in the root directory which may have a
		// limit on the number of files.
		String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
		File directory = new File(directoryName);
		if (!directory.isDirectory()) {
			if (!directory.mkdirs()) {
				return false;
			}
		}
		return directory.canWrite();
	}
	public static String getFileName (String string, String stringSplit){
		String[] temp = string.split(stringSplit);
		temp[temp.length-1] = temp[temp.length-1].trim();
		return temp[temp.length-1];
	}
	public static boolean hasStorage() {
		return hasStorage(true);
	}

	// make temp folder to write
	public static String makeTempFolder(){
		File savePath = new File(Environment.getExternalStorageDirectory() + Constants.MOBGAME_PATH_GAME);
		if (!savePath.exists()) savePath.mkdirs();
		String pathFolder = String.valueOf(savePath);
		return pathFolder;
	}

	// Make cached folder
	public static void makeCacheFolder(){
		File savePath = new File(Environment.getExternalStorageDirectory() + Constants.MOBGAME_CACHE_PATH);
		savePath.mkdirs();
	}

	// Remove all other temporary scanned files
	public static void removeTempContents(){
		File f = new File(Environment.getExternalStorageDirectory() + Constants.MOBGAME_CACHE_PATH);
		if(f.exists()){
			if(f.isDirectory()){
				File[] fs = f.listFiles();
				for (File file : fs) {
					file.delete();
				}
			}
			f.delete();
		}
	}
}
